package com.jiangxiaofei.citycloud.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jiangxiaofei.citycloud.R;
import com.jiangxiaofei.citycloud.activity.village.VillageDetailsActivity;
import com.jiangxiaofei.citycloud.bean.VillageNewsBean;
import com.jiangxiaofei.citycloud.util.ImgUtils;

import java.util.List;

/**
 * Created by JiangXFei on 2017/8/17.
 */

public class VillageNewsAdapter extends BaseAdapter {
    private Context mContext;
    private List<VillageNewsBean> vList;

    public VillageNewsAdapter(Context mContext, List<VillageNewsBean> vList) {
        this.mContext = mContext;
        this.vList = vList;
    }

    @Override
    public int getCount() {
        return vList.size();
    }

    @Override
    public Object getItem(int i) {
        return vList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_village_list, null, false);
            viewHolder = new ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.tv_title.setText(vList.get(i).getTitle());
        Integer type = Integer.valueOf(vList.get(i).getCategory());
        viewHolder.tv_type.setText(mContext.getResources().getStringArray(R.array.village_menu)[type==0?0:type-1]);
        Glide.with(mContext).load(ImgUtils.getImg(vList.get(i).getImg())).into(viewHolder.img_photo);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, VillageDetailsActivity.class);
                int id = vList.get(i).getId();
                intent.putExtra(VillageDetailsActivity.NEWSID, id);
                mContext.startActivity(intent);
            }
        });
        return view;
    }

    private class ViewHolder {
        TextView tv_title, tv_type;
        ImageView img_photo;

        public ViewHolder(View v) {
            tv_title = v.findViewById(R.id.village_tv_title);
            tv_type = v.findViewById(R.id.village_tv_type);
            img_photo = v.findViewById(R.id.village_img_photo);
        }
    }
}
